package read_input;

import java.util.ArrayList;

import model.Data;
import model.Stock;

/**
 * this class read the input and save all the data
 * @author Orit
 *
 */
public class Input {
	
	 /* Training phase: 012345678_012345678.exe -t days datafolder filenames modelfile
	  * Prediction phase: 012345678_012345678.exe -p days datafolder filenames modelfile buyfile*/
	Data data  = Data.get();
	//ArrayList<Stock> allStocks =  new ArrayList<Stock>(); 
	//----C'tor ----//
	/**
	 * default c'tor. read the input data from file. 

	 * @param args 
	 */
	public Input(String[] args) {
		if (args[0].trim().equals("-t")){
			trainingInput(args[1],args[2],args[3],args[4]);
		}else if (args[0].trim().equals("-p")){
			predictionInput(args[1],args[2],args[3],args[4],args[5]);
		}
	}
	
	private void trainingInput(String days, String datafolder, String filenames,String modelfile) {
		data.setPhase(Data.Phase.Train);		
		setInput(days,datafolder,filenames,modelfile);
	}
	
	private void predictionInput(String days, String datafolder, String filenames,
											  String modelfile, String buyfile) {
		data.setPhase(Data.Phase.Predict);
		data.setBuyfile(buyfile);
		setInput(days,datafolder,filenames,modelfile);
	}

	private void setInput(String days, String datafolder, String filenames,String modelfile){
		data.setModelfile(modelfile);
		data.setDays(Integer.valueOf(days));
		ArrayList<String > filesNames = new SimpleFileReader(filenames).readFile();
		CSVFileReader csvReader  = new CSVFileReader();
		for (String name : filesNames){
			csvReader.setFileName(datafolder + "/" + name);
			csvReader.readFile();
			data.getAllStocks().add(createStock(csvReader.getClosePrice(),name));
		}
		//setNumberOfDaysInPast(data.getAllStocks());
	}	
	
	/*private void setNumberOfDaysInPast(ArrayList<Stock> allStocks) {
		if (!allStocks.isEmpty()){
			data.setNumberOfDaysInPast(allStocks.get(0).getClosePrice().size());
		}
	}*/

	private Stock createStock(ArrayList<Double> closePrice,String stockname) {
		//double fitness = closePrice.get(data.getDays()) - closePrice.get(0);
		double profit = closePrice.get(closePrice.size()-1) - 
						closePrice.get(closePrice.size()-1-data.getDays());
		return new Stock(profit,stockname,closePrice);
	}
}
